<template>
  <view class="container">
  </view>
</template>

<script>
  import {
    sendSmsCode
  } from '../../api/auth'
  import {
    getLink,
    socialLogin,
    scanCodeClock
  } from "@/api/user.js";
  import {
    mapState,
    mapGetters
  } from "vuex"
  export default {
    data() {
      return {}
    },
    computed: {
      ...mapState(["userInfo", "userType", "userId"]),
    },
    onLoad(options) {

      // console.log("url",location.href)
      // #ifdef H5
      if (!options.code) {
        // let url = location.href.slice(0, location.href.indexOf("#"));          // console.log("url",url)
        // 1. 路径不能带端口号
        // # 号问题要注意, 项目最好是history 模式
        // let url = 'www.baidu.com/?teachtId=111'
        let url = window.location.href
        // let url = 'http://127.0.0.1/pages/login/index?qrCoedStr='+options.qrCoedStr
        getLink({
          type: 31,
          redirectUri: url
        }).then(res => {
          console.log("res", res.data)
          window.location.href = res.data

        })
      } else {
        // console.log("options",options)
        const code = options.code;
        const state = options.state;
        const type = 31;
        socialLogin({
          code,
          state,
          type
        }).then(res => {
          console.log("111", res)
          this.$store.dispatch('Login', {
            type: 10,
            data: res.data
          }).then(async () => {

            if (this.userType == 4 && options.qrCoedStr) {
             let result = await scanCodeClock({
                clockUserId:this.userId,
                qrCoedStr:options.qrCoedStr
              });

              if (result.code == 0) {
                return uni.redirectTo({
                  url: '/pages/user/scuess',
                })
              }
            }
            uni.$u.toast('登录成功')
            setTimeout(() => {
              uni.switchTab({
                url: '/pages/user/user'
              })
            }, 300)
          })
        }).catch(res => {
          console.log("res", res)
          if (res.data.code = '1002000005') {
            uni.reLaunch({
              url: `/pages/login/account?socialCode=${code}&socialState=${state}&socialType=${type}`
            }) // this.formData.socialCode = code;              // this.formData.socialState = state;              // this.formData.socialType = type;
          }
        })

      }
      // #endif
    },
    onReady() {

    },
    methods: {
      handleModeChange(index) {
        if (index !== this.currentModeIndex) {
          this.currentModeIndex = index
          this.$refs.form.clearValidate()
        }
      },
      codeChange(text) {
        this.codeTips = text
      },
      getCode() {
        const mobile = this.formData.mobile
        if (!mobile) {
          uni.$u.toast('请填写手机号')
        } else if (!uni.$u.test.mobile(mobile)) {
          uni.$u.toast('手机号格式不正确')
        } else if (this.$refs.uCode.canGetCode) {
          // 模拟向后端请求验证码
          uni.showLoading({
            title: '正在获取验证码'
          })

          //scene:1登陆获取验证码场景
          sendSmsCode({
            mobile: mobile,
            scene: 1
          }).then(res => {
            //console.log(res)
            uni.hideLoading()
            uni.$u.toast('验证码已发送')
            // 通知验证码组件内部开始倒计时
            this.$refs.uCode.start()
          })
        } else {
          uni.$u.toast('倒计时结束后再发送')
        }
      },
      handleSubmit() {
        this.$refs.form.validate().then(res => {
          this.mobileLogin(this.formData)
          // uni.login({
          //   provider: 'weixin',
          //   success: res => {
          //     let data = this.formData
          //     data.socialType = 34 //WECHAT_MINI_APP 先指定固定值
          //     data.socialCode = res.code
          //     data.socialState = Math.random() // 该参数没有实际意义暂时传随机数
          //     this.mobileLogin(data)
          //   },
          //   fail: res => {
          //     this.mobileLogin(this.formData)
          //   }
          // })
        })
      },
      mobileLogin(data) {
        console.log('login')
        this.$store.dispatch('Login', {
          type: this.currentModeIndex,
          data: data
        }).then(res => {
          uni.$u.toast('登录成功')
          setTimeout(() => {
            uni.switchTab({
              url: '/pages/user/user'
            })
          }, 300)
        })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
